// pages/sendorder/sendorder.js
const { $Toast } = require('../../dist/base/index');
const { $Message } = require('../../dist/base/index');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isrule:true,
    userInfo:{},
    title:"",
    name:"",
    tel:"",
    code:"",
    toAddress:"一期宿舍,1栋",
    room:"",
    getAddress:"顺丰",
    arrivetime:"9:00-12:00",
    price:2,
    array: ['顺丰', '邮政', '圆通', '中通'],
    index: 0,
    array2: ['9:00-12:00', '12:00-18:00', '18:00-21:00'],
    index2: 0,
    multiArray: [['一期宿舍', '二期宿舍', '三期宿舍', '四期宿舍', '五期宿舍'], ['1栋', '2栋', '3栋', '4栋', '5栋', '6栋', '7栋']],
    multiIndex: [0, 0]
  },

  //下单
  sendOrder(){
    this.data.isrule = true;
    if(this.data.tel.length != 11){
      this.data.isrule = false;
      $Message({
        content: '请输入11位的手机号码',
        type: 'error'
      });
    }
    if(this.data.code.length == 0){
      this.data.isrule = false;
      $Message({
        content: '请输入取货码',
        type: 'error'
      });
    }

    if(this.data.isrule == true){
      var app = getApp();
      var baseurl = app.globalData.BaseUrl;
      var token = app.globalData.token;
      var user = app.globalData.userInfo;
      var that = this;
      wx.showModal({
        title: '确认下单',
        success (res) {
          if (res.confirm) {
            wx.request({
              url: baseurl+'/api/exorder',
              data:{
                title:that.data.title,
                fromid:that.data.userInfo.id,
                toaddress:that.data.toAddress+","+that.data.room,
                getaddress:that.data.getAddress,
                code:that.data.code,
                tel:that.data.tel,
                booktime:that.data.arrivetime,
                money:that.data.price,
                state:0
              },
              header:{
                'content-type': 'application/x-www-form-urlencoded', // 默认值
                'token':token
              },
              method:'POST',
              success:(res)=>{
                if(res.data.code == 0){
                  //扣除相应费用
                  wx.request({
                    url: baseurl+'/api/user/update',
                    data:{
                      id:user.id,
                      money:(Number(user.money) - Number(that.data.price)).toFixed(1)
                    },
                    header:{
                      'content-type': 'application/x-www-form-urlencoded', // 默认值
                      'token':token
                    },
                    method:'POST',
                    success:(res)=>{
                      //用户数据修改
                      app.globalData.userChange = true;
                      if(res.data.code == 0){
                        
                      }else{
                        console.log("失败")
                      }
                    }
                  })
                  wx.switchTab({
                    url: '../myorder/myorder',
                  })
                }else{
                  $Toast({
                    content: '余额不足',
                    type: 'warning'
                  });
                }
              }
            })
          } else if (res.cancel) {
          }
        }
      })
    }
    
  },
  //订单标题
  changeTitle(e){
    this.setData({
      title:e.detail.detail.value
    })
  },
  //姓名
  changeName(e){
    this.setData({
      name:e.detail.detail.value
    })
  },
  //联系电话
  changeTel(e){
    this.setData({
      tel:e.detail.detail.value
    })
  },
  //取货码
  changeCode(e){
    this.setData({
      code:e.detail.detail.value
    })
  },
  //房间号
  changeRoom(e){
    this.setData({
      room:e.detail.detail.value
    })
  },

  //代取价格
  priceChang:function(e){
    this.setData({
      price:e.detail.value.toFixed(1)
    })
    //console.log(e.detail.value)
  },
  priceChanging:function(e){
    this.setData({
      price:e.detail.value.toFixed(1)
    })
    //console.log(e.detail.value)
  },

  //取货地址
  bindPickerChange: function(e) {
    //console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value,
      getAddress:this.data.array[e.detail.value],
    })
    console.log(this.data.getAddress)
  },
  //预约送达
  bindPickerChange2: function(e) {
    //console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index2: e.detail.value,
      arrivetime:this.data.array2[e.detail.value],
    })
    console.log(this.data.arrivetime)
  },
  //取货地址
  bindMultiPickerChange: function (e) {
    //console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      multiIndex: e.detail.value,
      toAddress:this.data.multiArray[0][this.data.multiIndex[0]]+","+this.data.multiArray[1][this.data.multiIndex[1]]
    })
    console.log(this.data.toAddress)
  },
  bindMultiPickerColumnChange: function (e) {
    //console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    
    switch(e.detail.column){
      case 0:
        switch (data.multiIndex[0]) {
          case 0:
            data.multiArray[1] = ['1栋', '2栋', '3栋', '4栋', '5栋', '6栋', '7栋'];
            break;
          case 1:
            data.multiArray[1] = ['9栋', '10栋', '11栋', '12栋', '13栋', '14栋', '15栋'];
            break;
          case 2:
            data.multiArray[1] = ['16栋', '17栋', '18栋', '19栋', '20栋', '21栋', '22栋'];
            break;
          case 3:
            data.multiArray[1] = ['23栋', '24栋', '25栋', '26栋', '27栋', '28栋'];
            break;
          case 4:
            data.multiArray[1] = ['29栋', '30栋'];
            break;            
        }
        data.multiIndex[1] = 0;
    }           
    //console.log(data.multiIndex);
    this.setData(data);
    //console.log(this.data.multiArray[0][this.data.multiIndex[0]]+","+this.data.multiArray[1][this.data.multiIndex[1]])
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var app = getApp();
    var userInfo = app.globalData.userInfo
    this.setData({
      userInfo:userInfo,
      name:userInfo.user,
      tel:userInfo.tel
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})